package com.esint.validation.controller;

import com.esint.validation.entity.ajax.AjaxResult;
import com.esint.validation.entity.car.CarUser;
import com.esint.validation.service.CarUserService;
import com.esint.validation.validators.CarUserAdd;
import com.esint.validation.validators.CarUserUpdate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @description: 测试controller类
 * @author: 谷明
 * @createDate: 2021/8/12 19:25
 */
@RestController
@RequestMapping(value = "/vtest")
public class TestController {

    @Autowired
    private CarUserService carUserService;

    //不分组校验写法
    /*@RequestMapping(value = "/adduser", method = RequestMethod.POST)
    public AjaxResult AddCarUser(@RequestBody @Valid CarUser carUser){
        carUserService.AddNewUser(carUser);
        return AjaxResult.GetOperSuccessObj();
    }*/

    //分组校验写法：一个字段可以在新增的时候和修改的时候指定不同的规则
    @RequestMapping(value = "/adduser", method = RequestMethod.POST)
    public AjaxResult AddCarUser(@RequestBody @Validated({CarUserAdd.class}) CarUser carUser){
        carUserService.AddNewUser(carUser);
        return AjaxResult.GetOperSuccessObj();
    }

    @RequestMapping(value = "/updateuser", method = RequestMethod.POST)
    public AjaxResult UpdateCarUser(@RequestBody @Validated({CarUserUpdate.class}) CarUser carUser){
        carUserService.AddNewUser(carUser);
        return AjaxResult.GetOperSuccessObj();
    }

}
